<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html>
	<head>
		<title>dimension-config-update</title>
		<%@ include file="../include/common.jsp"%>
		<%@ include file="../include/form.jsp"%>
	   <%@ include file="../include/tree.jsp"%>
		<link rel="stylesheet" media="all" type="text/css"
			href="${pageContext.request.contextPath}/style/default/tree/zTreeStyle.css" />
		<style type="text/css">
.ztree {
	margin-top: 5px;
	border: 1px solid #617775;
	background: #fff;
	width: 220px;
	height: 350px;
	overflow-y: scroll;
	overflow-x: auto;
}
</style>
<script type="text/javascript">
    var isRelation='${dimensionType.isRelation}';
    function showDimensionTree(){
    var cityObj=$("#parentName");
    var cityOffset=$("#parentName").offset();
    var rTree = $.fn.zTree.getZTreeObj("dimensionTree");
    if(rTree!=null){
        var nodes = rTree.getSelectedNodes();
        if(nodes.length==0){
			 var defaultNode = rTree.getNodeByParam("id", '${dimensionParent.id}', null);
			 rTree.selectNode(defaultNode);
		}
       	$("#dimensionContent").css({left:cityOffset.left + "px", top:cityOffset.top + cityObj.outerHeight() + "px"}).slideDown("fast");
		$("body").bind("mousedown", onRelTreeDown);
    }	
  }
  function onRelTreeDown(event) {
			if (!(event.target.id == "menuBtn" || event.target.id == "parentId" || event.target.id == "dimensionContent" || $(event.target).parents("#dimensionContent").length>0)) {
				$("#dimensionContent").fadeOut("fast");
				$("body").unbind("mousedown", onRelTreeDown);
			}
   }
   
   function relNodeClick(e, treeId, treeNode) {
			$("#dimensionContent").fadeOut("fast");
			$("body").unbind("mousedown", onRelTreeDown);
			$("#parentId").attr("value", treeNode.id);
			$("#parentName").attr("value",treeNode.name);
			$("#parentName").blur();
   }
  var dimensionTreeConfig={
      	async: {enable: true,
					url:"dimension-config!dimensionTree.action?dimensionId="+${dimension.id},
					dataType: "text",
					autoParam:["id"]
				},
			callback: {
				onClick: relNodeClick
			},
				view: {
					dblClickExpand: false,
					showLine: false
  }};
  function edit(){
	//校验(url+账号)是否唯一
	   var flag=true;
      if('${dimension.dimensionName}'
      ==$("#dimensionName").val()){
         flag=false;
      }
	  $.ajax({
		   type: "POST", 
		   url: "${pageContext.request.contextPath}/config/dimension-config!validateDimensionName.action", //请求的action
		   data: "dimensionName="+$("#dimensionName").val()+"&isValideFlag="+flag, //传的参数
		   dataType: 'json',
		   success: function(data){ //结果
			   if(data == true){
				   //提交
					 $.ajax({
						   type: "POST", 
						   url: "${pageContext.request.contextPath}/config/dimension-config!save.action", //请求的action
						   data: $("#myform").serialize(), //传的参数
						   dataType: 'json',
						   success: function(data){ //结果
							   if(data == true){
								   var pWin = getTopWin(window).currentWin["dimension-config-update"];
								   pWin.datagrid();
								   getTopWin(window).closejBox("dimension-config-update");
							   }
						   }
					});
			   }else{
				   getTopWin(window).getMessageBox().alert("您添加的维度类型已经存在","提示");
			   }
		   }
	});
  }
    $(function(){
	  $.formValidator.initConfig({formid:"myform"
          ,onerror:function(msg){}
          ,onsuccess:function(){
        	  		edit();
          			return false;
          }
      });
	  $("#dimensionName").formValidator({onshow:"请输入字符串 长度(1-32)",onfocus:"请输入字符串 长度(1-32)",oncorrect:"输入正确"}).inputValidator({min:1,onerror:"不能为空"}).inputValidator({empty:{leftempty:false,rightempty:false,emptyerror:"两边不能有空格"},onerror:"不能为空"})
	  .functionValidator({
			    fun:function(val,elem){
			    	    if(val.replace(/[^\x00-\xff]/g, "xx").length > 32){
			    	        return "长度超出最大范围";
			    	    }
			    	    return true;
				}
	  });
	  if(isRelation==1){
	     $.fn.zTree.init($("#dimensionTree"),dimensionTreeConfig);
	   }
  });
   function closewin(){
	   getTopWin(window).closejBox("dimension-config-update");
   }
  </script>
	</head>
	<body class="padding-8">
		<form action="" id="myform" name="myform" method="post">
			<table class="table_eidt">
				<tr>
					<td class="td_left" width="120px">
						<span class="span_alert">*</span>维度类型
					</td>
					<td class="td_right">
						${dimensionType.dimensionTypeName }
						<input type="hidden" id="dimensionTypeId" name="dimensionTypeId" value="${dimensionType.id}" />
						<input type="hidden" id="dimensionState" name="dimensionState" value="0" />
						<input type="hidden" name="id" id="id" value="${dimension.id}" />
					</td>
				</tr>
				<tr>
					<td class="td_left" width="120px">
						<span class="span_alert">*</span>维度名称
					</td>
					<td class="td_right">
						<input type="text" name="dimensionName" id="dimensionName"
							class="input_text" value="${dimension.dimensionName }"/>
						<span id="dimensionNameTip" style="width: 150px"></span>
					</td>
				</tr>
			   <tr>
			     <td class="td_left" width="120px">
						父级维度对象
					</td>
					<td class="td_right">
					<c:choose>
				   <c:when test="${dimensionType.isRelation==1}">
					<input type="text" name="parentName" id="parentName"
							class="input_text"
							onfocus="showDimensionTree();return false;" value="${dimensionParent.dimensionName }" />
					<input type="hidden" name="parentId" id="parentId"
							class="input_text" value="${dimensionParent.id }" />
				   </c:when>
				  <c:otherwise>
					         <select id="parentId" name = "parentId"  class = "select" style="width:227px;" >
					         <c:forEach var="dimensionList" items="${dimensionList}" varStatus="var">
					         <c:choose>
					           <c:when test="${dimensionList.id eq dimension.id}">
					              <option value="${dimensionList.id }" selected="selected">${dimensionList.dimensionName}</option>
					           </c:when>
					           <c:otherwise>
					              <option value="${dimensionList.id }">${dimensionList.dimensionName}</option>
					           </c:otherwise>
					         </c:choose>
					         </c:forEach>
					   </select>
					     </c:otherwise>
					</c:choose>
					</td>
				</tr>
			</table>
			<div align="center" style="margin-top: 9px;">
				<input type="submit" id="button" name="button" value="保存"
					class="btn-4">
				<input type="reset" class="btn-4">
				<input type="button" value="取消" onclick="closewin();" class="btn-4">
			</div>
		</form>
	<div id="dimensionContent" class="menuContent"
			style="display: none; position: absolute;">
			<ul id="dimensionTree" class="ztree"
				style="margin-top: 0; padding-left: 0; width: 220px; height: 155px;"></ul>
		</div>
	</body>
</html>
